package com.base.cn.platform.os.controller.order.goods;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.order.PayType;
import com.base.cn.platform.os.service.course.cou.buy.CourseBuyRecordService;
import com.base.cn.platform.os.service.manage.order.goods.OrderDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台订单流水管理Controller
 *
 * @author s.li
 * @create 2018-05-15-14:17
 */
@Controller
@RequestMapping("/admin/order/details")
public class AdminOrderDetailsController extends AdminBaseController {

    @Autowired
    private OrderDetailsService orderDetailsService;
    @Autowired
    private CourseBuyRecordService courseBuyRecordService;

    /**
     * 赠送流水列表页面
     *
     * @return
     */
    @RequestMapping("/queryFreeDetailsList")
    public String queryFreeDetailsList() {
        return resultPage("/order/details/free-details-list");
    }

    /**
     * 加载赠送流水数据列表
     *
     * @param model
     * @param params
     * @param currentPage
     * @return 数据页面
     */
    @RequestMapping("/ajaxQueryFreeDetailsList")
    public String ajaxQueryFreeDetailsList(Model model, @RequestBody Map<String, Object> params,
                                           @RequestParam("currentPage") int currentPage) {
        params.put("payTypeList", "'GIVING','COURSE_CART','MEMBER'");
        Map<String, Object> dataMap = orderDetailsService.findOrderDetailsPage(params, currentPage, true, true, false);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/order/details/ajax-free-details-list");
    }


    /**
     * 销售记录管理
     *
     * @return 列表页面
     */
    @RequestMapping("/queryPaySuccessOrderDetailsList")
    public String queryPaySuccessOrderDetailsList() {
        return resultPage("/order/details/pay-success-details-list");
    }

    /**
     * 加载订单数据列表
     *
     * @param params      查询条件
     * @param currentPage 页码
     * @return 数据列表页面
     */
    @RequestMapping("/ajaxQueryPaySuccessOrderDetailsList")
    public String ajaxQueryPaySuccessOrderDetailsList(Model model,
                                                      @RequestBody Map<String, Object> params,
                                                      @RequestParam("currentPage") int currentPage) {
        params.put("statusList", "'SUCCESS','CLOSE'");
        params.put("notPayType", "'MEMBER'");
        Map<String, Object> dataMap = orderDetailsService.findOrderDetailsPage(params, currentPage, true, true, false);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/order/details/ajax-pay-success-details-list");
    }

    /**
     * 退费记录列表
     *
     * @return
     */
    @RequestMapping("/queryRefundOrderDetailsList")
    public String queryRefundOrderDetailsList() {
        return resultPage("/order/details/refund-details-list");
    }

    /**
     * 加载订单数据列表
     *
     * @param params      查询条件
     * @param currentPage 页码
     * @return 数据列表页面
     */
    @RequestMapping("/ajaxQueryRefundOrderDetailsList")
    public String ajaxQueryRefundOrderDetailsList(Model model,
                                                  @RequestBody Map<String, Object> params,
                                                  @RequestParam("currentPage") int currentPage) {
        params.put("detailStatus", "REFUND");
        Map<String, Object> dataMap = orderDetailsService.findOrderDetailsPage(params, currentPage, true, true, false);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/order/details/ajax-refund-details-list");
    }

    /**
     * 查询流水详情
     *
     * @param id 流水ID
     * @return 详情页面
     */
    @RequestMapping("/findOrderDetailsInfo")
    public String findOrderDetailsInfo(Model model,
                                       @RequestParam("id") BigDecimal id) {
        Map<String, Object> details = orderDetailsService.findOrderDetailsInfo(id, true, true, true);
        model.addAttribute("details", details);
        return resultPage("/order/details/details-info");
    }

    /**
     * 修改流水状态
     *
     * @param id            流水ID
     * @param detailsStatus 状态
     * @return Map<String , Object>
     */
    @RequestMapping("/updateOrderDetailsStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.ORDER, action = "修改订单流水状态")
    public Map<String, Object> updateOrderDetailsStatus(HttpServletRequest request, @RequestParam("id") BigDecimal id,
                                                        @RequestParam("detailsStatus") String detailsStatus) {
        BigDecimal sysUserId = loginUtil.getLoginUserId(request);
        return orderDetailsService.updateOrderDetailsStatus(sysUserId, id, detailsStatus);
    }

    /**
     * 初始化延期页面
     *
     * @param detailsId 流水ID
     * @return 操作页面
     */
    @RequestMapping("/ajaxDelayDetails")
    public String ajaxDelayDetails(Model model,
                                   @RequestParam("detailsId") BigDecimal detailsId) {
        Map<String, Object> courseBuyRecord = courseBuyRecordService.findCourseBuyRecordByDetailsId(detailsId, true);
        model.addAttribute("courseBuyRecord", courseBuyRecord);
        return resultPage("/order/details/delay-details");
    }

    /**
     * 修改课程延期
     *
     * @param validTime 时间
     * @param detailsId 流水ID
     * @return Map<String , Object>
     */
    @RequestMapping("/updateCourseBuyRecordValidTime")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.ORDER, action = "修改订单课程延期")
    public Map<String, Object> updateCourseBuyRecordValidTime(@RequestParam("validTime") String validTime,
                                                              @RequestParam("detailsId") BigDecimal detailsId) {
        return courseBuyRecordService.updateCourseBuyRecordValidTime(detailsId, validTime);
    }


    /**
     * @param payType     支付类型
     * @param keyword     手机订单号
     * @param distinguish
     * @throws Exception
     */
    @RequestMapping("/downloadExcelOrdersDetailsList")
    @OpLog(module = AdminModuleEnum.ORDER, action = "导出销售/退费记录订单")
    public ResponseEntity<byte[]> downloadExcelOrdersDetailsList(@RequestParam(value = "payType", required = false) String payType,
                                                                 @RequestParam(value = "keyword", required = false) String keyword,
                                                                 @RequestParam(value = "beginPayTime", required = false) String beginPayTime,
                                                                 @RequestParam(value = "endPayTime", required = false) String endPayTime,
                                                                 @RequestParam("distinguish") String distinguish
                                                                 ,@RequestParam(value = "gainDay",required = false) Integer gainDay
                                    ,@RequestParam(value = "shopName",required = false,defaultValue = "") String shopName) throws Exception {
        //数据导出
        Map<String, Object> params = new HashMap<>();

        params.put("payType", payType);
        params.put("keyword", keyword);
        params.put("gainDay", gainDay);
        params.put("shopName", shopName);
        if (distinguish.equals("SALES")) {
            params.put("beginPayTime", beginPayTime);
            params.put("endPayTime", endPayTime);
            params.put("statusList", "'SUCCESS','CLOSE'");
            params.put("notPayType", "'" + PayType.PAY_TYPE_FREE + "','" + PayType.PAY_TYPE_COURSE_CART + "','" + PayType.PAY_TYPE_MEMBER + "'");
        } else if (distinguish.equals("REFUND")) {
            params.put("detailStatus", "REFUND");
            params.put("beginUpdateTime", beginPayTime);
            params.put("endUpdateTime", endPayTime);
        }
        return orderDetailsService.downloadExcelOrdersDetailsList(params, 2000, distinguish);
    }

}


